{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一阶段_第三周作业"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第八步. 测试与结果保存"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第七步调优后，最优结果为-0.5811165772791611，比第5步的-0.5805192306115483的正则调优还差了一些，按道理第八步再进行正则调优一次。但是由于计算资源的问题，在此就不再继续做下去了。以后有时间，再细细调优了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "dpath = './data/'\n",
    "#train = pd.read_csv(dpath +\"RentListingInquries_FE_train.csv\")\n",
    "test = pd.read_csv(dpath +\"RentListingInquries_FE_test.csv\")\n",
    "test.head()\n",
    "\n",
    "#test_id = test['listing_id']\n",
    "#X_test = test.drop([ \"listing_id\"], axis=1)\n",
    "#X_test = test.drop([ \"listing_id\"], axis=1)\n",
    "\n",
    "X_test = np.array(test)\n",
    "\n",
    "# drop ids and get labels\n",
    "#y_train = train['interest_level']\n",
    "#X_train = np.array(train.drop(\"interest_level\", axis=1))\n",
    "\n",
    "# drop ids and get labels\n",
    "# drop ids and get labels\n",
    "#y_test = test['interest_level']\n",
    "#X_test = np.array(test.drop(\"interest_level\", axis=1))\n",
    "#X_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "f2 = open('best_model.plk','rb')\n",
    "s2 = f2.read()\n",
    "best_model = pickle.loads(s2)\n",
    "\n",
    "\n",
    "y_test_pred = best_model.predict_proba(X_test)\n",
    "\n",
    "out_df1 = pd.DataFrame(y_test_pred)\n",
    "out_df1.columns = [\"high\", \"medium\", \"low\"]\n",
    "out_df1.to_csv(\"best_model.csv\", index=False)\n",
    "\n",
    "#out_df = pd.concat([test_id,out_df1], axis = 1)\n",
    "#out_df.to_csv(\"best_model.csv\", index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
